#!coding:utf-8
# vim: tabstop=4 shiftwidth=4 softtabstop=4


import unittest
from cvirt.cnode.test.testConstant import FAKE_DOM
from cvirt.cnode.vm import vm_utils
from cvirt.cnode.test import fakeLibvirt
from cvirt.cnode.vm import libvirtDriver
from cvirt.common.test import BaseTest


libvirtDriver.libvirt = fakeLibvirt

class TestLibvirtDriver(BaseTest):

    def setUp(self):
        super(TestLibvirtDriver, self).setUp()

        self.host = "127.0.0.1"

        self.driver = libvirtDriver.Driver()
        self.driver._conn = fakeLibvirt.Connection("test",False)
        self.instance = {
                "uuid":"12312312312312313",
                "vcpu" : 1,
                "name":"i-%08.f" % 1L,
                "memory_mb" : 1024,
                "instance_path":"/home/ezzze/instances/i-0000001/",
                "has_cdrom":False,
                "need_launch":False,
                "graphics_port":5900,
                "need_intel_hda":False,
                "graphics_type":"vnc",
        }

        self.image = {
            "image_id":1L,
            "name":"test",
            "os_type":"WindowsXP"
        }
        self.network = [{
            "bridge":"br0",
            "mac" : '123123',
            "model":"virtio",
            "type":"bridge"

        }]
        self.disk = [{
            "disk_name":"test1",
            "disk_dev":"vda",
            "disk_type":"cdrom",
            "disk_bus":"virtio",
            "disk_format":"qcow2",
            "disk_path":self.instance['instance_path'] + 'disk',
            "disk_size":8,
            "disk_cache":None
        }]


    def tearDown(self):
        super(TestLibvirtDriver, self).tearDown()

        vm_utils.clean(self.instance['instance_path'])

    def testlistInsts(self):
        self.assertEqual(self.driver.list_instance_ids(),[1])

    def testCreateVm(self):
        xml = self.driver.create(self.instance,self.network,self.disk)

        self.assertEqual(xml,FAKE_DOM)

    def testStartVm(self):

        is_start = self.driver.start(self.instance['name'])
        self.assertEqual(is_start,True)

    def testShutdownVm(self):

        is_shutdown = self.driver.soft_shutdown(self.instance['name'])
        self.assertEqual(is_shutdown,True)

    def testRebootVm(self):
        is_reboot = self.driver.soft_reboot(self.instance['name'])

        self.assertEqual(is_reboot,True)

    def testDestroy(self):
        is_destory = self.driver.destroy(self.instance['id'],self.instance['instance_path'])

        self.assertEqual(is_destory,True)











